package com.odontosis.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import com.odontosis.as.OdontosisDataAccessObject;
import com.odontosis.entidade.Paciente;
import com.odontosis.entidade.Servico;
import com.odontosis.util.HibernateUtil;

public class InformarPagamentoDAO extends OdontosisDataAccessObject<Paciente> {
	
	/**
	 * Recupera servi�os pendentes.
	 * 
	 * @param idPaciente
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Servico> recuperarServicosPendentes(Long idPaciente) {
		HibernateUtil.closeSession();
		Session session = HibernateUtil.currentSession();
		String hql = "select s from Servico s " + "join s.paciente p "
				+ "where p.id = :idPaciente and " + "s.pago = 0";
		Query query = session.createQuery(hql);
		return query.list();
	}

	/**
	 * Recupera pacientes com servi�os pendentes.
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Paciente> recuperarPacientesServicosPendentes(String nome,
			String numero) {
		HibernateUtil.closeSession();
		Session session = HibernateUtil.currentSession();		
		String hql = "select distinct p from Paciente p "
				+ "join p.servicos s " + 
				"where s.pago = 0 " +
				"and s.ativo = 1";
		if (StringUtils.isNotBlank(nome))
			hql += "and p.nome like :nomePaciente ";
		if (StringUtils.isNotBlank(numero))
			hql += "and p.numeroPaciente like :numeroPaciente ";

		Query query = session.createQuery(hql);
		if (StringUtils.isNotBlank(nome)) {
			query.setParameter("nomePaciente", "%" + nome + "%" );
		}
		if (StringUtils.isNotBlank(numero)) {
			query.setParameter("numeroPaciente", "%" + numero + "%" );
		}
		return query.list();
	}
}
